#!/usr/bin/env python
try:
    import json
except:
    from helper import HelpJson as json
import sys
import class_set
import helper

class ResourceMapper(helper.MapperHelper):
    """
    from user2resource.log
    get tvid and vlike
    """
    def mapper(self):
        for line in sys.stdin:
            left, recom_tag = self.loader(line)
            if (left is not None) and (recom_tag is not None):
                self.allocator(left, recom_tag)
                self.allocator(self.join(left, recom_tag.getRelatedId()), recom_tag)

    def loader(self, line):
        line = line.strip()
        node_operator = json.loads(line)
        left = None
        recom_tag = None
        try:
            type = node_operator["type"]
            aid = node_operator["tvid"]
            uid = node_operator["uid"]
            tm = node_operator["addtime"]
            if (type == "vlike"):
                left = uid
                recom_tag = class_set.RecomTagFeed('resource')
                recom_tag.setTime(tm)
                recom_tag.setRelatedId(aid)
                recom_tag.setUid(uid)
            else:
                pass
        except KeyError:
            pass
        return (left, recom_tag)

def main():
    rm = ResourceMapper()
    rm.mapper()

if __name__ == '__main__':
    main()
